#region Copyright RenGuiYou. All rights reserved.

//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================

#endregion

using UnityEngine;

namespace Pandora.Console
{
    public abstract class ScrollWindowBase : IConsoleWindow
    {
        public static float TitleWidth = 240f;
        private Vector2 m_ScrollPosition = Vector2.zero;

        public virtual void Initialize(params object[] args)
        {
        }

        public virtual void Shutdown()
        {
        }

        public virtual void OnEnter()
        {
        }

        public virtual void OnLeave()
        {
        }

        public virtual void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
        }

        private Vector2 oldInfoDrag;

        public void OnDraw()
        {
            getDownPos();

            Vector2 drag = getDrag();
            if ((drag.x != 0) && (downPos != Vector2.zero))
            {
                m_ScrollPosition.x -= (drag.x - oldInfoDrag.x);
            }

            if ((drag.y != 0) && (downPos != Vector2.zero))
            {
                m_ScrollPosition.y += (drag.y - oldInfoDrag.y);
            }

            oldInfoDrag = drag;
            m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition);
            {
                OnDrawScrollableWindow();
                GUILayout.Space(PdrConsoleWindow.spaceHeight * 2);
            }
            GUILayout.EndScrollView();
        }

        protected abstract void OnDrawScrollableWindow();

        protected void DrawItem(string title, string content)
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(title, GUILayout.Width(TitleWidth));
                GUILayout.Label(content);
            }
            GUILayout.EndHorizontal();
        }


        Vector2 downPos;

        void getDownPos()
        {
            if (Application.platform == RuntimePlatform.Android ||
                Application.platform == RuntimePlatform.IPhonePlayer)
            {
                if (Input.touches.Length == 1 && Input.touches[0].phase == TouchPhase.Began)
                {
                    downPos = Input.touches[0].position;
                }
            }
            else
            {
                if (Input.GetMouseButtonDown(0))
                {
                    downPos.x = Input.mousePosition.x;
                    downPos.y = Input.mousePosition.y;
                }
            }
        }

        Vector2 mousePosition;

        Vector2 getDrag()
        {
            if (Application.platform == RuntimePlatform.Android ||
                Application.platform == RuntimePlatform.IPhonePlayer)
            {
                if (Input.touches.Length != 1)
                {
                    return Vector2.zero;
                }

                return Input.touches[0].position - downPos;
            }
            else
            {
                if (Input.GetMouseButton(0))
                {
                    mousePosition = Input.mousePosition;
                    return mousePosition - downPos;
                }
                else
                {
                    return Vector2.zero;
                }
            }
        }
    }
}